/* See LICENSE file for copyright and license details. */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "util.h"

static FILE *log_fd;

void
die(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
		fputc(' ', stderr);
		perror(NULL);
	} else {
		fputc('\n', stderr);
	}

	exit(1);
}

void *
ecalloc(size_t nmemb, size_t size)
{
	void *p;

	if (!(p = calloc(nmemb, size)))
		die("calloc:");
	return p;
}

FILE *
getlogfd()
{
	return log_fd;
}
void 
loginit()
{
	log_fd = fopen("/tmp/dwm.console.log", "a+");
	LOG_INFO("----------------------------------- %s ----------------------------------", "new start");
}

void get_time_str(char *time_dst, int len) {
    time_t tm;
    struct tm *tms;
    char time_str[TIME_STR_LEN];
    time(&tm);
    tms = gmtime(&tm);
    sprintf(time_str, "%hu-%hu-%hu %hu:%hu:%hu",
        (unsigned short) 1900+tms->tm_year,  
		(unsigned short) 1+ tms->tm_mon, 
		(unsigned short)  tms->tm_mday, 
        (unsigned short) 8 + tms->tm_hour, 
		(unsigned short)  tms->tm_min, 
		(unsigned short)  tms->tm_sec);
    strncpy(time_dst, time_str, MIN(len, TIME_STR_LEN));
}